"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 资源池化
Case Name   : 执行计划(explain):关联查询
Create At   : 2023.5
Owner       : opentestcase009
Description :
    1、查看集群状态
    2、创建表,插入数据
    3、关闭seqscan; set enable_seqscan to off;
        explain结合select语句,使用表的inner join连接方式打印执行信息
        explain结合select语句,使用表的left join连接方式打印执行信息
        explain结合select语句,使用表的right join连接方式打印执行信息
        explain结合select语句,使用表的full join连接方式打印执行信息
        explain结合select语句,使用表的cross join连接方式打印执行信息
    4、打开seqscan; set enable_seqscan to on;
        explain结合select语句,使用表的inner join连接方式打印执行信息
        explain结合select语句,使用表的left join连接方式打印执行信息
        explain结合select语句,使用表的right join连接方式打印执行信息
        explain结合select语句,使用表的full join连接方式打印执行信息
        explain结合select语句,使用表的cross join连接方式打印执行信息
    5、清理环境
Expect      :
    1、集群状态正常,数据库节点状态正常
    2、建表成功,插入数据成功
    3、关闭seqscan,结合不同连接方式打印执行结果成功
    4、开启seqscan,结合不同连接方式打印执行结果成功
    5、清理环境成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.comsh = CommonSH('PrimaryDbUser')
        self.t1_name = 't_dms_page_019_1'
        self.t2_name = 't_dms_page_019_2'

    def test_shared_storage(self):
        text = '-----step1:查看集群状态; expect:集群状态正常-----'
        self.log.info(text)

        status = self.comsh.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '-----step2:创建表,插入数据; expect:建表成功,插入数据成功-----'
        self.log.info(text)

        sql = f'''create table {self.t1_name} (name varchar(10),stu_id integer
            not null,score int) with(segment=on);
            create table {self.t2_name} (cname varchar(10),cid varchar(5) 
            not null, num int, sname varchar(10)) with(segment=on);
            insert into {self.t1_name} values('A-lin',01,88),('Baby',02,95),
            ('Cindy',03,100);
            insert into {self.t2_name} values('math',01,66,'A-lin'),
            ('English',02,88,'Baby'),('Science',03,95,'Cindy');'''
        self.log.info(sql)
        res = self.comsh.execut_db_sql(sql)
        self.log.info(res)
        self.assertTrue(res.count(self.constant.CREATE_TABLE_SUCCESS) == 2
                        and res.count(self.constant.INSERT_SUCCESS_MSG) == 2,
                        '执行失败' + text)

        text = '----step3:关闭seqscan,explain结合select语句使用,打印执行信息; expect:成功----'
        self.log.info(text)

        self.log.info('-----关闭seqscan,结合inner join的explain执行信息-----')
        explain1 = f'''set enable_seqscan to off;
            explain performance select * from {self.t1_name} inner join 
            {self.t2_name} on {self.t1_name}.name = {self.t2_name}.sname;'''
        self.log.info(explain1)

        exec_exp1 = self.comsh.execut_db_sql(explain1)
        self.log.info(exec_exp1)
        self.assertIn('Hash Join', exec_exp1, '执行结果异常')

        self.log.info('-----关闭seqscan,结合left join的explain执行信息-----')
        explain2 = f'''set enable_seqscan to off;
            explain performance select * from {self.t1_name} left join 
            {self.t2_name} on name = sname;'''
        self.log.info(explain2)

        exec_exp2 = self.comsh.execut_db_sql(explain2)
        self.log.info(exec_exp2)
        self.assertIn('Hash Left Join', exec_exp2, '执行结果异常')

        self.log.info('-----关闭seqscan,结合right join的explain执行信息-----')
        explain3 = f'''set enable_seqscan to off;
            explain performance select * from {self.t1_name} right join
            {self.t2_name} on name = sname;'''
        self.log.info(explain3)

        exec_exp3 = self.comsh.execut_db_sql(explain3)
        self.log.info(exec_exp3)
        self.assertIn('Hash Right Join', exec_exp3, '执行结果异常')

        self.log.info('-----关闭seqscan,结合full join的explain执行信息-----')
        explain4 = f'''set enable_seqscan to off;
            explain performance select * from {self.t1_name} full join
            {self.t2_name} on name = sname;'''
        self.log.info(explain4)

        exec_exp4 = self.comsh.execut_db_sql(explain4)
        self.log.info(exec_exp4)
        self.assertIn('Hash Full Join', exec_exp4, '执行结果异常')

        self.log.info('-----关闭seqscan,结合cross join的explain执行信息-----')
        explain5 = f'''set enable_seqscan to off;
            explain performance select * from {self.t1_name} cross join
            {self.t2_name} where name = sname;'''
        self.log.info(explain5)

        exec_exp5 = self.comsh.execut_db_sql(explain5)
        self.log.info(exec_exp5)
        self.assertIn('Hash Join', exec_exp5, '执行结果异常')

        text = '----step5:打开seqscan,explain结合select语句使用,打印执行信息; expect:成功----'
        self.log.info(text)

        self.log.info('-----打开seqscan,结合inner join的explain执行信息-----')
        explain6 = f'''set enable_seqscan to on;
            explain performance select * from {self.t1_name} inner join 
            {self.t2_name} on {self.t1_name}.name = {self.t2_name}.sname;'''
        self.log.info(explain6)

        exec_exp6 = self.comsh.execut_db_sql(explain6)
        self.log.info(exec_exp6)
        self.assertIn('Hash Join', exec_exp6, '执行结果异常')

        self.log.info('-----打开seqscan,结合left join的explain执行信息-----')
        explain7 = f'''set enable_seqscan to on;
            explain performance select * from {self.t1_name} left join 
            {self.t2_name} on name = sname;'''
        self.log.info(explain7)

        exec_exp7 = self.comsh.execut_db_sql(explain7)
        self.log.info(exec_exp7)
        self.assertIn('Hash Left Join', exec_exp7, '执行结果异常')

        self.log.info('-----打开seqscan,结合right join的explain执行信息-----')
        explain8 = f'''set enable_seqscan to on;
            explain performance select * from {self.t1_name} right join
            {self.t2_name} on name = sname;'''
        self.log.info(explain8)

        exec_exp8 = self.comsh.execut_db_sql(explain8)
        self.log.info(exec_exp8)
        self.assertIn('Hash Right Join', exec_exp8, '执行结果异常')

        self.log.info('-----打开seqscan,结合full join的explain执行信息-----')
        explain9 = f'''set enable_seqscan to on;
            explain performance select * from {self.t1_name} full join
            {self.t2_name} on name = sname;'''
        self.log.info(explain9)

        exec_exp9 = self.comsh.execut_db_sql(explain9)
        self.log.info(exec_exp9)
        self.assertIn('Hash Full Join', exec_exp9, '执行结果异常')

        self.log.info('-----打开seqscan,结合cross join的explain执行信息-----')
        explain10 = f'''set enable_seqscan to on;
            explain performance select * from {self.t1_name} cross join
            {self.t2_name} where name = sname;'''
        self.log.info(explain10)

        exec_exp10 = self.comsh.execut_db_sql(explain10)
        self.log.info(exec_exp10)
        self.assertIn('Hash Join', exec_exp10, '执行结果异常')

    def tearDown(self):
        text = '-----step6:清理环境,删除表; expect:清理环境成功-----'
        self.log.info(text)

        clear = f'''drop table {self.t1_name} cascade;
            drop table {self.t2_name} cascade;'''
        self.log.info(clear)
        clear_res = self.comsh.execut_db_sql(clear)
        self.log.info(clear_res)

        self.assertEqual(clear_res.count(self.constant.DROP_TABLE_SUCCESS), 2,
                         '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
